<?php
if (!defined('IN_WONDERSHARE')){
	die('Hacking attempt');
}

class ProductReviewModule{

	private $db;
   
	public function __construct(){
		$this->db = $GLOBALS['db'];
	}

	/**
	 * 取得评论列表
	 * @param integer $web_type 站点类型
	 * @param integer $check_state 审核状态
	 * @param integer $offset 翻页偏移量
	 * @param integer $limit 每页显示数
	 * @param integer $num 总条数
	 * @param string $s_where 查询条件
	 * return array
	 * */
	public function get_list( $web_type, $check_state, $offset, $limit, &$num, $s_where = '' ){
		$sql = "SELECT SQL_CALC_FOUND_ROWS d.*, p.product_name FROM product_reviews_detail AS d
				LEFT JOIN won_product AS won ON d.pid = won.id
				LEFT JOIN product AS p ON won.won_product_id = p.product_id
				WHERE check_state = {$check_state} AND won.web_type = {$web_type} {$s_where}
				ORDER BY did DESC LIMIT {$offset}, {$limit}
		";
		
		$ret = $this->db->getAll( $sql );
		$num = $this->db->getNum();
		return $ret;
	}
	
	
	/**
	 * 根据产品名取得产品ID
	 * @param string $pname 产品名
	 * @return string
	 * */
	public function get_pid_by_name( $pname ){
		$sql = "SELECT product_id FROM product WHERE product_name LIKE '%{$pname}%'";
		$data = $this->db->getAll( $sql );
		
		$arr = array();
		foreach( $data as $v ){
			$arr[] = $v['product_id'];
		}
		return implode( ',', $arr );
	}
	
	
	/**
	 * 审核评论
	 * @param string $did_str 评论id字符串
	 * @param integer $check_state 审核状态
	 * @return boolean
	 * */
	public function check_state( $did_str, $check_state ){
		$sql = "UPDATE product_reviews_detail SET check_state = {$check_state}, check_user = '{$_SESSION['AName']}', check_date = now() WHERE did IN ({$did_str})";
		return $this->db->query( $sql );
	}

	
	/**
	 * 删除评论
	 * @param string $did 评论id
	 * @return boolean
	 * */
	public function del( $did ){
		$sql = "DELETE FROM product_reviews_detail WHERE did = {$did}";
		return $this->db->query( $sql );	
	}
	
	
	/**
	 * 推荐与取消推荐
	 * */
	public function rec( $did, $pid, $is_rec ){
		$sql = "UPDATE product_reviews_detail SET is_rec = 0 WHERE pid = {$pid}";
		$this->db->query( $sql );
		
		if( $is_rec == 1 ){
			$sql = "UPDATE product_reviews_detail SET is_rec = 1 WHERE did = {$did}";
			$this->db->query( $sql );
		}
		
		return true;
	}
	
	/**
	 * 取得待审核的评论数
	 * @param integer $web_type 站点类型
	 * @return integer
	 * */
	public function get_wait_check( $web_type ){
		$sql = "SELECT COUNT(*) AS num FROM product_reviews_detail
				WHERE check_state = 0 AND web_type = {$web_type}
		";
		return $this->db->getOne( $sql );
	}
	
	
	/**
	 * 向表Table中插入数据
	 * */
	public function add_table( $url, $pid, $pname, $web_type ){
		
		$t = $_SERVER['REQUEST_TIME'];
		
		switch ( (int)$web_type ){
			case 1;//ws
				$sql = "SELECT COUNT(*) num FROM Table_44 WHERE Field_21624 = {$pid}";
				$sql_2 = "INSERT INTO Table_44( url, CreatorID, MenderID, CreateTime, MendTime, Field_21624, Field_21625 )
					VALUE('{$url}', 1, 1, 0, {$t}, {$pid}, '{$pname}')";
			break;
			case 2://amersfot
				$sql = "SELECT COUNT(*) num FROM Table_186 WHERE Field_21969 = {$pid}";
				$sql_2 = "INSERT INTO Table_186( url, CreatorID, MenderID, CreateTime, MendTime, Field_21969, Field_21970 )
					VALUE('{$url}', 1, 1, 0, {$t}, {$pid}, '{$pname}')";
			break;
			case 8://iskysoft
				$sql = "SELECT COUNT(*) num FROM Table_193 WHERE Field_22043 = {$pid}";
				$sql_2 = "INSERT INTO Table_193( url, CreatorID, MenderID, CreateTime, MendTime, Field_22043, Field_22044 )
					VALUE('{$url}', 1, 1, 0, {$t}, {$pid}, '{$pname}')";
			break;
		}

		$num = $this->db->getOne( $sql );
		if( $num ) return;
		$this->db->query( $sql_2 );
		return true;
	}
	
	/**
	 * 从表Table_44中删除数据
	 * */
	public function del_table( $pid, $web_type ){
		
		switch ( (int)$web_type) {
			case 1://ws
				$sql = "DELETE FROM Table_44 WHERE Field_21624 = {$pid}";
			break;
			case 2://aimersoft
				$sql = "DELETE FROM Table_186 WHERE Field_21969 = {$pid}";
			break;
			case 8://iskysoft
				$sql = "DELETE FROM Table_193 WHERE Field_22043 = {$pid}";
			break;

		}
		
		$this->db->query( $sql );
		return true;
	}
	
	
	/**
	 * 更新表生成时间
	 * @param integer $pid 产品id
	 * @param integer $web_type 网站类型
	 * */
	public function set_time( $pid, $web_type ){
		$time = time();
		switch( (int)$web_type ){
			case 1://ws
				$sql_1 = "UPDATE Table_26 SET MendTime = '{$time}' WHERE Field_20044 = {$pid}";
				$sql_2 = "UPDATE Table_44 SET MendTime = '{$time}' WHERE Field_21624 = {$pid}";
				$sql_3 = "UPDATE Table_45 SET MendTime = '{$time}' WHERE Field_20350 = {$pid}";
			break;
			case 2://aimersoft
				$sql_1 = "UPDATE Table_174 SET MendTime = '{$time}' WHERE Field_21895 = {$pid}";
				$sql_2 = "UPDATE Table_186 SET MendTime = '{$time}' WHERE Field_21969 = {$pid}";
				$sql_3 = "UPDATE Table_175 SET MendTime = '{$time}' WHERE Field_21901 = {$pid}";
			break;
			case 8://iskysoft
				$sql_1 = "UPDATE Table_137 SET MendTime = '{$time}' WHERE Field_21669 = {$pid}";
				$sql_2 = "UPDATE Table_193 SET MendTime = '{$time}' WHERE Field_22043 = {$pid}";
				$sql_3 = "UPDATE Table_147 SET MendTime = '{$time}' WHERE Field_21726 = {$pid}";
			break;
		}
		$this->db->query( $sql_1 );
		$this->db->query( $sql_2 );
		$this->db->query( $sql_3 );
	}
	
	
	/**
	 * 更新评论数
	 * @param integer $pid 产品ID
	 * @param integer $star 星数
	 * */
	public function update_review_num( $pid, $star ){
	
		$sql = "SELECT COUNT(*) num FROM product_reviews WHERE pid = {$pid}";
		$num = $this->db->getOne( $sql );
	
		if( $num ){
			$sql = "UPDATE product_reviews SET star_{$star} = star_{$star} + 1 WHERE pid = {$pid}";
			$ret = $this->db->query( $sql );
		}else{
			$sql = "INSERT INTO product_reviews SET pid = {$pid}, star_{$star} = 1";
			$this->db->query( $sql );;
		}
	}
	
	
	/**
	 * 审核未通过或从通过状态下直接删除
	 * @param integer $pid 产品ID
	 * */
	public function check_no_pass( $pid, $star ){
		$sql = "UPDATE product_reviews SET star_{$star} = star_{$star} - 1 WHERE pid = {$pid}";
		$ret = $this->db->query( $sql );
	}
	
	
	/**
	 * 取得回复信息
	 * */
	public function get_reply( $did ){
		$sql = "SELECT * FROM product_reviews_detail WHERE did = {$did}";
		return $this->db->getOneRs( $sql );
	}
	
	/**
	 * 设置回复信息
	 * */
	public function set_reply( $did, $reply ){
		$sql = "UPDATE product_reviews_detail SET reply = '{$reply}' WHERE did = {$did}";
		return $this->db->query( $sql );
	}
	
	/**
	 * 根据评论ID取得评论
	 * */
	public function get_review_info( $did ){
		$sql = "SELECT * FROM product_reviews_detail WHERE did = {$did}";
		return $this->db->getOneRs( $sql );
	}
	
	/**
	 * 根据产品ID取得产品信息
	 * */
	public function get_product_info( $pid ){
		$sql = "SELECT p.product_name, p.product_variant FROM won_product won_p 
				LEFT JOIN product p ON p.product_id = won_p.won_product_id 
				WHERE won_p.id = {$pid}";
		return $this->db->getOneRs( $sql );
	}
	
	/**
	 * 取得某产品的最新一条评论 
	 * */
	public function get_newest_review( $pid ){
		$sql = "SELECT username, review FROM product_reviews_detail WHERE pid = {$pid} ORDER BY did DESC LIMIT 1";
		return $this->db->getOneRs( $sql );
	}
	
	/**
	 * 转发客服成功
	 * */
	public function transpood_ok( $did ){
		$sql = "UPDATE product_reviews_detail SET is_transpood = 1 WHERE did = {$did}";
		return $this->db->query( $sql );
	}
	
	
	/**
	 * 有些星级数错误，调用此函数更新星级数
	 * */
	public function sys_star_num( $pid ){
		$sql = "SELECT star, COUNT(*) AS num FROM product_reviews_detail WHERE pid = {$pid} and check_state = 1 GROUP BY star";
		$data = $this->db->getAll( $sql );
			
		$arr = array();
		foreach( $data as $v ){
			$arr[] = 'star_'.$v['star'].'='.$v['num'];
		}
		$str = implode(',', $arr);
		
		$sql = "UPDATE product_reviews set {$str} WHERE pid = {$pid}";
		$this->db->query( $sql );
		return $str;
	}
	
	
}